<?php

namespace Biz\model\brand;

/**
 * 品牌内容池
 * Class Content
 * @package Biz\model\brand
 */
class Content extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'brand_content';
    
    public const SEARCH_TYPE_BRAND_NAME = 1;
    public const SEARCH_TYPE_BRAND_ALIAS = 2;
    public const SEARCH_TYPE_BRAND_KEYWORDS = 3;

    const CLAIM_CONTENT_NONE = 0; //初始状态
    const CLAIM_CONTENT_DONE = 1;  //已经收录
    const CLAIM_CONTENT_IGNORE = 2; //忽略
    const CLAIM_CONTENT_CANCEL = 3; //取消收录
    const CLAIM_CONTENT_MATCH = 4; //匹配,待确认
    const CLAIM_CONTENT_DESC_MAP = [
        self::CLAIM_CONTENT_NONE => '已重置',
        self::CLAIM_CONTENT_DONE => '已经收录',
        self::CLAIM_CONTENT_IGNORE => '已忽略',
        self::CLAIM_CONTENT_CANCEL => '取消收录',
        self::CLAIM_CONTENT_MATCH => '待认领',
    ];

    const CLAIM_NONE_MATCH = 1; //没有未匹配的关联
    const CLAIM_WAIT_MATCH = 2; //含有未匹配的关联
    const CLAIM_MATCH = 3; //确认匹配的关联

    const MATCH_SELF = 1; //品牌发的内容
    const MATCH_CONTENT_HIT = 2; //内容命中
    const MATCH_TAG_HIT = 4; //标签命中
    const MATCH_WIKI_TITLE = 8; //商品名称命中
    const MATCH_BRAND_WIKI = 16; //品牌商品命中

    const FEATURED_SELECT = 1; //精选
    const FEATURED_NONE = 0; //普通

    const AUDIT_STATUS_NONE = 0; //未审核
    const AUDIT_STATUS_SUCCESS = 1; //审核通过
    const AUDIT_STATUS_REJECT = 2; //审核未通过
    const AUDIT_STATUS_AUTO = 3; //自动收录
    const AUDIT_STATUS_WAIT = 4; //审核中
    const ALL_AUDIT_STATUS = [
        self::AUDIT_STATUS_NONE,
        self::AUDIT_STATUS_SUCCESS,
        self::AUDIT_STATUS_REJECT,
        self::AUDIT_STATUS_AUTO,
    ];
    const AUDIT_MAP = [
        self::AUDIT_STATUS_NONE => '未审核',
        self::AUDIT_STATUS_REJECT => '审核未通过',
    ];
    const B_AUDIT_MAP = [
        self::AUDIT_STATUS_NONE => '审核中',
        self::AUDIT_STATUS_REJECT => '审核未通过',
    ];

    const CLAIM_TYPE_NONE = 0; //正常状态
    const CLAIM_TYPE_CONTENT_ONLY = 1; //仅认领内容
    const CLAIM_TYPE_WIKI_ONLY = 2; //认领商品
    const CLAIM_TYPE_APPEND_ONLY = 3; //仅关联商品
    const CLAIM_TYPE_BOTH = 4; //认领内容并关联商品
    const CLAIM_TYPE_MAP = [
        self::CLAIM_TYPE_CONTENT_ONLY => '仅认领内容',
        self::CLAIM_TYPE_WIKI_ONLY => '认领商品',
        self::CLAIM_TYPE_APPEND_ONLY => '仅关联商品',
        self::CLAIM_TYPE_BOTH => '认领内容并关联商品',
    ];

    public static function getToppedIds($brandId)
    {
        return self::select('id', [
            'brand_id' => $brandId,
            'brand_uid' => 0,
            'claim_content_status' => self::CLAIM_CONTENT_DONE,
            'audit_status[!]' => self::AUDIT_STATUS_REJECT,
            'top_time[>]' => 0,
            'ORDER' => ['top_time' => 'DESC']
        ]);
    }


    public static function countClaim($brand_id)
    {
        return $brand_id > 0 ? self::count([
            'brand_id' => $brand_id,
            'brand_uid' => 0,
            'claim_content_status' => self::CLAIM_CONTENT_DONE,
            'audit_status[!]' => self::AUDIT_STATUS_REJECT,
        ]) : 0;
    }


    public static function getById($id)
    {
        return self::get("*", array(
            "id" => $id
        ));
    }

    public static function getByBidAndOid($obj_id, $brand_id)
    {
        return self::get('*', [
            'brand_id' => $brand_id,
            'obj_id' => $obj_id,
            'claim_content_status' => self::CLAIM_CONTENT_DONE
        ]);
    }

    public static function getAllByBidAndOid($obj_id, $brand_id)
    {
        return self::select('*', [
            'brand_id' => $brand_id,
            'obj_id' => $obj_id,
            'claim_content_status' => self::CLAIM_CONTENT_DONE
        ]);
    }

    public static function getContentByObjId($objId)
    {
        return self::select("*", [
            'obj_id' => $objId,
            'brand_uid' => 0,
            'claim_content_status' => self::CLAIM_CONTENT_DONE,
            'audit_status[!]' => self::AUDIT_STATUS_REJECT,
        ]);
    }
    
    public static function geCountNum($where)
    {
        return self::count($where);
    }
}